'use strict';
var data = [];
for (let i = 0; i < 20; i++) {
  data.push({
    id: i + 2,
    phone: getMoble(),
    title: "动漫学院" + i,
    head: "张泽" + i,
  })
}

function getMoble() {
  var prefixArray = new Array("130", "131", "132", "133", "135", "137", "138", "170", "187", "189");
  var i = parseInt(10 * Math.random());
  var prefix = prefixArray[i];
  for (var j = 0; j < 8; j++) {
    prefix = prefix + Math.floor(Math.random() * 10);
  }
  return prefix
}

var baseData = data


const Controller = require('egg').Controller;

class BasicResourcesController extends Controller {
  async index() {
    const { ctx, ctx: { service, customBody }, app: { mysql } } = this;

    const userInfo = ctx.userInfo
    let uid = userInfo.id;
    let { pageSize = 10, page = 1 } = customBody;
    let limit = pageSize,
      offset = pageSize * (page - 1);
    var res = JSON.parse(JSON.stringify(baseData))
    var data = res.splice(offset, limit)
    // var data = await mysql.select('category', { where: { uid, is_deleted: 0 }, limit, offset })
    // var total = await mysql.select('category', { where: { uid, is_deleted: 0 } })



    let total = baseData.length;
    ctx.body = {
      code: 200,
      data,
      total,
      meg: "获取成功"
    };
  }

  async store() {
    const { ctx, ctx: { service, customBody }, app: { mysql } } = this;
    const userInfo = ctx.userInfo
    customBody.uid = userInfo.id;
    customBody.is_show = 1;


    var id = Number(baseData[baseData.length - 1].id) + 1;
    baseData.push(
      {
        id,
        ...customBody
      }
    )
    ctx.body = {
      code: 200,
      data: null,
      meg: "添加成功"
    };
    if (data.affectedRows) {

    } else {
      ctx.body = {
        code: 200,
        data: null,
        meg: "添加失败"
      };
    }
  }

  async remove() {
    const { ctx, ctx: { service, customBody }, app: { mysql } } = this;
    var { ids } = customBody;
    var datas = ids.split(',')
    datas = datas.map(val =>
      Number(val)
    )
    var list = []
    baseData.forEach(val => {
      if (datas.indexOf(val.id) < 0) {
        list.push(val)
      }
    })
    baseData = list
    ctx.body = {
      code: 200,
      data: null,
      meg: "删除成功"
    };
  }


  async detail() {
    const { ctx, ctx: { service, customBody }, app: { mysql } } = this;
    let { id } = customBody
    const userInfo = ctx.userInfo
    let uid = userInfo.id;
    const data = await mysql.get('category', { id });
    ctx.body = {
      code: 200,
      data,
      meg: "获取成功"
    };
  }


}

module.exports = BasicResourcesController;





